package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.User;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegesitActivity extends AppCompatActivity {

    TextView username;
    TextView password;
    TextView phone;
    TextView address;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regesit);

        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegesitActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        username  = (TextView)findViewById(R.id.register_name);
        password = (TextView)findViewById(R.id.register_password);
        phone = (TextView)findViewById(R.id.register_phone);
        address = (TextView)findViewById(R.id.register_address);
        Button reg = (Button)findViewById(R.id.register_button);
        Button update = (Button)findViewById(R.id.update);
        intent = this.getIntent();
        //System.out.println(intent.getStringExtra("token"));
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update_test();
            }
        });

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert_test();
                Intent jump_intent =new Intent(RegesitActivity.this,MainActivity.class);;
                startActivity(jump_intent);
            }
        });
    }

    public void bind(int userid)
    {
        String url = "http://10.0.2.2:8443/api/bindUser";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userid + "\",\n" +
                "  \"token\": \"" + intent.getStringExtra("token")+ "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }

        });

    }

    private void update_test()
    {
        String url = "http://10.0.2.2:8443/api/updatebyid";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + "1" + "\",\n" +
                "  \"username\": \"" + username.getText().toString() + "\",\n" +
                "  \"password\": \"" + password.getText().toString() + "\",\n" +
                "  \"address\": \"" + address.getText().toString() + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    private void insert_test()
    {
        String url = "http://10.0.2.2:8443/api/register";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"username\": \"" + username.getText().toString() + "\",\n" +
                "  \"password\": \"" + password.getText().toString() + "\",\n" +
                "  \"phone\": \"" + phone.getText().toString() + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String url = "http://10.0.2.2:8443/api/login";
                OkHttpClient mOkHttpClient = new OkHttpClient();

                String json = "{\n" +
                        "  \"username\": \"" + username.getText().toString() + "\",\n" +
                        "  \"password\": \"" + password.getText().toString() + "\"\n" +
                        "}";


                Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

                mOkHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("测试", e+"");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Gson gson = new Gson();
                        String rep = response.body().string();
                        System.out.println(rep);
                        Type type = new TypeToken<Result<User>>(){}.getType();
                        Result<User> result = gson.fromJson(rep,type);
                        System.out.println(intent.getStringExtra("token"));
                        if(intent.getStringExtra("token") != null)
                        {
                            bind(result.getData().getUserid());
                        }
                    }

                });
            }
        });
    }


}